package com.lsp.rabbitmqcousumer.direct;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(
                        value = "TestAckQueue"
                ),
                exchange = @Exchange(
                        value = "TestAckExchange"),
                key =  "TestAckRouting")
)
public class AckReceiver {

    @RabbitHandler
    public void receiverAck(String msg, Message message, Channel channel)  throws Exception{
        System.out.println("----------HelloSender收到消息：" + msg + "当前时间：" + LocalDateTime.now());
        try {
//            int a = 1/0;
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//            System.out.println("receiver success");
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            System.out.println("receiver Reject");
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            System.out.println("receiver fail");
        }
    }
}
